import moment from 'dayjs'
import { BasicColumn, FormSchema } from "/@/components/Table";
import { render } from "/@/utils/common/renderUtils";
import { AutoComplete } from 'ant-design-vue';
import { usePermission } from '/@/hooks/web/usePermission';

const { hasPermission } = usePermission();

//订单列表 - 状态转译
const statusFmt = ({text}: { text: any }) => {
  if (!text) {
    return
  }
  const color = text == '0' ? 'green' : text == '1' ? 'red' : 'gray';
  return render.renderTag(render.renderDict(text, 'ddztType'), color)
}

//订单列表 - 表头
export const columns: BasicColumn[] = [
  {title: '状态', dataIndex: 'ddzt', width: 60, customRender: statusFmt,},
  {title: '订单号', dataIndex: 'orderId', width: 100, className: 'text-red-500'},
  {
    title: '部门',
    dataIndex: 'ddlx',
    width: 63,
    ellipsis: true,
    customRender: ({text}) => render.renderDict(text, 'ddlxType'),
  },
  {
    title: '类型',
    dataIndex: 'ywlx',
    width: 63,
    ellipsis: true,
    customRender: ({text}) => render.renderDict(text, 'ywlxType'),
  },
  {title: '订单名称', dataIndex: 'travelLine', width: 130, align: 'left',},
  {title: '操作人', dataIndex: 'realname', width: 55},
  {title: '人数', dataIndex: 'number', width: 60, align: 'right'},
  {title: '应收', dataIndex: 'ygs', width: 70, align: 'right', className: 'text-red-500'},
  {title: '应付', dataIndex: 'ygf', width: 70, align: 'right', className: 'text-green-500'},
  {title: '已收', dataIndex: 'yjs', width: 70, align: 'right', className: 'text-red-500'},
  {title: '已付', dataIndex: 'yjf', width: 70, align: 'right', className: 'text-blue-500'},
  {title: '未收', dataIndex: 'wsk', width: 70, align: 'right', className: 'text-green-500'},
  {title: '未付', dataIndex: 'wfk', width: 70, align: 'right', className: 'text-red-500'},
  {title: '利润', dataIndex: 'lr', width: 70, align: 'right', className: 'text-blue-500'},
]

//订单添加
export const addSchema: FormSchema[] = [
  { label: '', field: 'id', component: 'Input', show: false },
  { label: "", field: "scheduleId", component: "Input", show: false },
  { label: "", field: "bankPid", component: "Input", show: false },
  {
    label: '部门',
    field: 'ddlx',
    component: 'JDictSelectTag',
    // componentProps: {
    //   dictCode: 'ddlxType@ddlxUser', 
    //   placeholder: ' ', 
    //   getPopupContainer: () => document.body,
    // },
    required: true
  },
  {
    label: '业务类型',
    field: 'ywlx',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'ywlxType', 
      placeholder: ' ',
      getPopupContainer: () => document.body,
    },
    required: true,
  },
               
  {
    label: '订单名称',
    field: 'travelLine',
    component: 'Input',
    componentProps: {placeholder: ' ', AutoComplete: 'off'},
    required: true,
  },
  {
    label: '订单名称11',
    field: 'travelLine11',
    component: 'JSelectInput',
    required: true,
  },
  {
    label: '订单名称',
    field: 'scheduleId',
    component: 'Input',
    componentProps: {placeholder: ' ', AutoComplete: 'off'},
    required: true,
  },
]

//TODO 收支添加
export const accountSchema: FormSchema[] = [
  { label: "", field: "id", component: "Input", show: false },
  { label: "", field: "orderId", component: "Input", show: false },
  { label: "", field: "businessId", component: "Input", show: false },
  { label: "", field: "coopId", component: "Input", show: false },
  {
    label: '收支类型',
    field: 'sflx',
    defaultValue: '1',
    colProps: { span: 14 },
    component: 'JDictSelectTag',
    rules: [{required: true, type: 'string', message: '请选择收支类型'}],
    componentProps: ({ formModel,formActionType }) => {
      return {
        dictCode: 'sflxType',
        placeholder: ' ',
        showChooseOption: false,
        type: 'radioButton',
        onChange: (value) => {
          //console.info("accountSchema@@@", value)
          const { clearValidate } = formActionType;
          //清除校验
          clearValidate();
          if (value == 2) {
            formModel.bankId = '';
            formModel.sfsj = '';
            formModel.bankPid = '';
            formModel.jePid = '';
          } else {
            formModel.sfsj = moment().format('YYYY-MM-DD');
          }
        },
      };
    },
  },
  {
    field: 'xnsz',
    label: '虚拟收支',
    component: 'Switch',
    colProps: { span: 5 },
    defaultValue: 0,
    componentProps: {
      checkedChildren: '是',
      checkedValue:1,
      unCheckedChildren: '否',
      unCheckedValue: 0
    },
    // dynamicDisabled: ({ values }) => {
    //   return hasPermission('business:virtaul');
    // },
    ifShow: ({ values }) => {
      return hasPermission('business:virtual');
    },
  },
  {
    label: '收支部门',
    field: 'ddlx',
    component: 'JDictSelectTag',
    rules: [{required: true, type: 'string', message: '请选择收支部门'}],
    colProps: {
      span: 22,
    },
    componentProps: ({ formModel }) => {
      return {
        dictCode: 'ddlxType@ddlxUser',
        placeholder: ' ',
        showChooseOption: false,
      };
    },
    show: false
  },
  {
    label: '采购单位',
    field: 'bcId',
    component: 'JSelectInput',
    componentProps: {
      getPopupContainer: () => document.body,
      dropdownMatchSelectWidth: false
    },
    rules: [{required: true, type: 'string'}],
    ifShow: ({ values }) => {
      return (values.sflx == '' || values.sflx == '1');
    },
    colProps: {
      span: 22,
    },
  },
  {
    label: '支付单位',
    field: 'cId',
    component: 'Cascader',
    componentProps: {
      getPopupContainer: () => document.body,
      //stringToNumber: true
    },
    ifShow: ({ values }) => {
      return values.sflx == '2';
    },
    dynamicRules: ({ model }) => {
      return [
        { required: true, message: '支付项目不能为空！' },
        { validator: (_, value) => {
            //console.info("dynamicRules", value)
            return new Promise((resolve, reject) => {
              if (value == '') {
                return resolve() //不与required重复
              } else if ( value.length == 3) {
                model.coopId = value[2]
                return resolve()
              } else {
                model.coopId = ''
                return reject('选择的项目不对请重新选择！');
              }
            });
          },
        },
      ]
    },
    colProps: {
      span: 22,
    },
  },
  {
    label: '收支方式',
    field: 'sffs',
    defaultValue: '1',
    rules: [{required: true, type: 'string', message: '请选择收支方式'}],
    colProps: {
      span: 22,
    },
    component: 'JDictSelectTag',
    componentProps: ({ formModel }) => {
      return {
        dictCode: 'sffsType',
        placeholder: ' ',
        showChooseOption: false,
        onChange: (value) => {
          //console.info("accountSchema@@@", value, formModel.sflx)
          // 支出
          if (formModel.sflx == 2) {
            formModel.bankId = '';
            formModel.sfsj = '';
          } else {
            formModel.sfsj = moment().format('YYYY-MM-DD');
          }
        },
      };
    },
  },
  {
    label: '收支银行',
    field: 'bankId',
    component: 'JDictSelectTag',
    componentProps: ({ formModel }) => {
      return {
        dictCode: "chee_bank@ddlxBank",
        placeholder: formModel.sflx == 1 && formModel.sffs == 3 ? '认款银行' : '收支银行',
        stringToNumber: true,
        showChooseOption: false,
        getPopupContainer: () => document.body,
        onChange: (value) => {
          // console.info("accountSchema@@@", value, formModel.sflx)
          formModel.bankPid = value;
        },
      };
    },
    colProps: {
      span: 22,
    },
    dynamicDisabled: ({ values }) => {
      return values.sflx == 2;
    },
    dynamicRules: ({ values }) => {
      return values.sflx == 1 ? [{required: true, message: '请选择收支银行'}] : [];
    },
  },
  // {
  //   label: '收支户名',
  //   field: 'dfhm',
  //   component: 'Input',
  //   componentProps: {
  //     AutoComplete: 'off',
  //     placeholder: ' ',
  //   },
  //   colProps: {
  //     span: 22,
  //   },
  // },
  {
    label: '收支金额',
    field: 'je',
    component: 'InputNumber',
    rules: [{required: true, type: 'number'}],
    componentProps: ({ formModel }) => {
      return {
        AutoComplete: 'off',
        placeholder: formModel.sflx == 1 && formModel.sffs == 3 ? '认款金额' : '收支金额',
        style: {
          width: '137px', 
          padding: '0 5px!important'
        }
      };
    },
    colProps: {
      span: 22,
    },
  },
  {
    label: '进账银行',
    field: 'bankPid',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: "chee_bank@ddlxBank",
      placeholder: "进账银行",
      stringToNumber: true,
      showChooseOption: false,
      getPopupContainer: () => document.body,
    },
    colProps: {
      span: 22,
    },
    ifShow: ({ values }) => {
      //return values.sflx == 1 && values.sffs == 3;
      return false;
    },
    dynamicRules: ({ values }) => {
      return values.sflx == 1 && values.sffs == 3 ? [{required: true, message: '请选择入账银行'}] : [];
    },
  },
  {
    label: '进账金额',
    field: 'jePid',
    component: 'InputNumber',
    rules: [{required: true, type: 'number'}],
    componentProps: ({ formModel }) => {
      return {
        AutoComplete: 'off',
        placeholder: '进账金额',
        style: {
          width: '137px', 
          padding: '0 5px!important'
        }
      };
    },
    colProps: {
      span: 22,
    },
    ifShow: ({ values }) => {
      return values.sflx == 1 && values.sffs == 3;
    },
    dynamicRules: ({ values }) => {
      return values.sflx == 1 && values.sffs == 3 ? [{required: true, message: '请选择入账金额'}] : [];
    },
  },
  {
    label: '收支时间',
    field: 'sfsj',
    component: 'DatePicker',
    defaultValue: moment().format('YYYY-MM-DD'),
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
    },
    dynamicDisabled: ({ values }) => {
      return values.sflx == 2;
    },
    dynamicRules: ({ values }) => {
      return values.sflx == 1 ? [{required: true, message: '请选择收支时间'}] : [];
    },
    colProps: {
      span: 22,
    },
  },
  {
    label: '备注',
    field: 'bz',
    component: 'InputTextArea',
    componentProps: {
      placeholder: ' ',
      rows: 5,
    },
    colProps: {
      span: 22,
    },
  },
]
